package cn.iocoder.yudao.module.worker.controller.admin.clerkapply;

import cn.iocoder.yudao.module.worker.controller.admin.category.vo.CategoryRespVO;
import cn.iocoder.yudao.module.worker.controller.app.clerkapply.vo.AppClerkApplySaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.worker.controller.admin.clerkapply.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerkapply.ClerkApplyDO;
import cn.iocoder.yudao.module.worker.service.clerkapply.ClerkApplyService;

@Tag(name = "管理后台 - 店员申请")
@RestController
@RequestMapping("/worker/clerk-apply")
@Validated
public class ClerkApplyController {

    @Resource
    private ClerkApplyService clerkApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建店员申请")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:create')")
    public CommonResult<Long> createClerkApply(@Valid @RequestBody ClerkApplySaveReqVO createReqVO) {
        return success(clerkApplyService.createClerkApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员申请")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:update')")
    public CommonResult<Boolean> updateClerkApply(@Valid @RequestBody ClerkApplySaveReqVO updateReqVO) {
        clerkApplyService.updateClerkApply(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改店员申请状态")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:update')")
    public CommonResult<Boolean> updateClerkApplyStatus(@Valid @RequestBody ClerkApplySaveReqVO reqVO) {
        clerkApplyService.updateClerkApplyStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:delete')")
    public CommonResult<Boolean> deleteClerkApply(@RequestParam("id") Long id) {
        clerkApplyService.deleteClerkApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:query')")
    public CommonResult<ClerkApplyRespVO> getClerkApply(@RequestParam("id") Long id) {
        ClerkApplyDO clerkApply = clerkApplyService.getClerkApply(id);
        return success(BeanUtils.toBean(clerkApply, ClerkApplyRespVO.class));
    }

    @GetMapping("/getWorkerGoodsList")
    @Operation(summary = "获得店员申请技能")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:query')")
    public CommonResult<List<CategoryRespVO>> getWorkerGoodsList(@RequestParam("id") Long id) {
        List<CategoryRespVO> list = clerkApplyService.getGoodsList(id);
        return success(BeanUtils.toBean(list, CategoryRespVO.class));
    }

    @PutMapping("/saveWorkerGoodsList")
    @Operation(summary = "保存店员申请技能")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:update')")
    public CommonResult<Boolean> saveWorkerGoodsList(@Valid @RequestBody ClerkApplySaveReqVO updateReqVO) {
        clerkApplyService.updateGoodsList(updateReqVO);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员申请分页")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:query')")
    public CommonResult<PageResult<ClerkApplyRespVO>> getClerkApplyPage(@Valid ClerkApplyPageReqVO pageReqVO) {
        PageResult<ClerkApplyDO> pageResult = clerkApplyService.getClerkApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClerkApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员申请 Excel")
    @PreAuthorize("@ss.hasPermission('worker:clerk-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClerkApplyExcel(@Valid ClerkApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClerkApplyDO> list = clerkApplyService.getClerkApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员申请.xls", "数据", ClerkApplyRespVO.class,
                        BeanUtils.toBean(list, ClerkApplyRespVO.class));
    }

}